<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>findPathService</title>
    <script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>

    <script type="text/javascript">
        var map, findPathService, findPathParameter, url = "http://localhost:8090/iserver/services/map-Dubai/rest/maps/Dubai";
        var serviceUrl = "http://localhost:8090/iserver/services/transportationAnalyst-Dubai/rest/networkanalyst/BuildNetwork@are-mu";
        map = L.map('map', {
            crs: L.CRS.EPSG4326,
            center: [25.08, 55.23],
            maxZoom: 18,
            zoom: 10
        });
        L.TileLayer.include({
            createTile: function (coords, done) {
                var tile = document.createElement('img');
                L.DomEvent.on(tile, 'load', L.Util.bind(this._tileOnLoad, this, done, tile));
                L.DomEvent.on(tile, 'error', L.Util.bind(this._tileOnError, this, done, tile));
                if (this.options.crossOrigin || this.options.crossOrigin === '') {
                    tile.crossOrigin = this.options.crossOrigin === true ? '' : this.options.crossOrigin;
                }
                tile.alt = '';
                tile.setAttribute('role', 'presentation');
                var xhr = new XMLHttpRequest();
                xhr.responseType = 'blob';
                xhr.addEventListener('loadend', function (evt) {
                    var data = this.response;
                    if (data !== undefined) {
                        tile.src = URL.createObjectURL(data);
                    } else {
                        tile.setState('error');
                    }

                });
                xhr.addEventListener('error', function () {
                    tile.setState('error');
                });
                xhr.open('GET', this.getTileUrl(coords));
                xhr.setRequestHeader('Accept', '*/*');
                xhr.setRequestHeader('X-HW-ID', 'userid');
                xhr.setRequestHeader('X-HW-APPKEY', 'username');
                xhr.send();
                return tile;
            }
        });
        L.supermap.tiledMapLayer(url, { noWrap: true }).addTo(map).once("load", function () {
            findPathProcess();
        });

        function findPathProcess() {
            //添加站点
            var marker1 = L.marker([25.2, 55.3]).addTo(map);
            var marker3 = L.marker([25.3, 55.4]).addTo(map);

            //创建最佳路径分析服务实例
            findPathService = L.supermap.networkAnalystService(serviceUrl, {
                headers: {
                    "X-HW-ID": "userid",
                    "X-HW-APPKEY": "username"
                }
            });
            //创建最佳路径分析参数实例
            var resultSetting = new SuperMap.TransportationAnalystResultSetting({
                returnEdgeFeatures: true,
                returnEdgeGeometry: true,
                returnEdgeIDs: true,
                returnNodeFeatures: true,
                returnNodeGeometry: true,
                returnNodeIDs: true,
                returnPathGuides: true,
                returnRoutes: true
            });
            var analystParameter = new SuperMap.TransportationAnalystParameter({
                resultSetting: resultSetting,
                weightFieldName: "SmLength"
            });
            findPathParameter = new SuperMap.FindPathParameters({
                isAnalyzeById: false,
                nodes: [L.point(55.3, 25.2), L.point(55.4, 25.3)],
                parameter: analystParameter
            });
            var myIcon = L.icon({
                iconUrl: "../img/walk.png",
                iconSize: [20, 20]
            });
            //进行查找
            findPathService.findPath(findPathParameter, function (serviceResult) {
                var result = serviceResult.result;
                console.log('result :', result);
                result.pathList.map(function (result) {
                    L.geoJSON(result.route).addTo(map);
                    L.geoJSON(result.pathGuideItems, {
                        pointToLayer: function (geoPoints, latlng) {
                            L.marker(latlng, { icon: myIcon }).addTo(map);
                        },
                        filter: function (geoJsonFeature) {
                            if (geoJsonFeature.geometry && geoJsonFeature.geometry.type === 'Point') {
                                return true;
                            }
                            return false;
                        }
                    }).addTo(map);
                })
            });
        }
    </script>
</body>

</html>